package com.quyeying.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quyeying.utils.TokenUtil;
import com.quyeying.utils.result.R;
import com.quyeying.utils.result.ResultEnum;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;
import com.quyeying.service.TblFollowMerchantService;
import com.quyeying.entity.TblFollowMerchantEntity;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RestController;

/**
* <p>
* tbl_follow_merchant 前端控制器
* </p>
* @author jll
* @since 2022-12-14
*/
@Slf4j
@RestController
@RequestMapping("followMerchantEntity")
public class TblFollowMerchantController {

    @Resource
    private TblFollowMerchantService tblFollowMerchantService;

    /**
    * 新增
    **/
    @PostMapping("/add")
    public R add(@RequestBody TblFollowMerchantEntity tblFollowMerchantEntity,HttpServletRequest request){
        long userId = TokenUtil.getUserId(request);
        try {
            tblFollowMerchantEntity.setUserId(userId);
            tblFollowMerchantService.add(tblFollowMerchantEntity);
        }catch (DuplicateKeyException duplicateKeyException) {
            QueryWrapper<TblFollowMerchantEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("merchant_id", tblFollowMerchantEntity.getMerchantId()).eq("user_id", userId);
            tblFollowMerchantService.remove(queryWrapper);
        }
        return R.success();
    }

    /**
    * 删除
    **/
    @DeleteMapping("/del")
    public R delete(Long id){
        try {
            return tblFollowMerchantService.delete(id);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 更新
    **/
    @PostMapping("/update")
    public R update(@RequestBody TblFollowMerchantEntity tblFollowMerchantEntity){
        try {
            return tblFollowMerchantService.updateData(tblFollowMerchantEntity);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 分页查询
    **/
    @GetMapping("/findListByIdPage")
    public R findListByIdPage(@RequestParam Integer current,
                            @RequestParam Integer size, HttpServletRequest request){
        try {
            return tblFollowMerchantService.findListByIdPage(current, size,request);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * id查询
    **/
    @GetMapping("/findById")
    public R findById(Long id){
        try {
            return tblFollowMerchantService.findById(id);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }
}
